<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/17 11:07
 */

namespace app\admin\controller;


use think\Facade\View;
use app\admin\validate\Login as LoginValidate;
use app\admin\business\Manager as BusManager;

class Index extends AdminBase
{
    public function index()
    {

        return View::fetch();
    }

    public function home()
    {
        $info = array(
            'web_server' => $_SERVER['SERVER_SOFTWARE'],
            'onload'     => ini_get('upload_max_filesize'),
            'think_v'    => app()->version(),
            'phpversion' => phpversion(),
        );
        return View::fetch('home', [
            'info' => $info
        ]);
    }

    /**
     * 功能说明 修改密码
     * @author: wang
     * @Date: 2020/3/31 17:08
     * @return string
     * @throws \Exception
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $password = $this->request->param('password', '', 'trim,htmlspecialchars');
            $password1 = $this->request->param('password1', '', 'trim,htmlspecialchars');
            $password2 = $this->request->param('password2', '', 'trim,htmlspecialchars');

            $data = [
                'password' => $password,
                'password1' => $password1,
                'password2' => $password2,
            ];

            $LoginValidate = new LoginValidate();
            if (!$LoginValidate->scene('edit')->check($data)) {
                return $this->show(config('status.error'), $LoginValidate->getError());
            }

            try {
                $result = BusManager::upPassword($this->adminUser, $data);
            } catch (\Exception $e) {
                return $this->show(config('status.error'), $e->getMessage());
            }
            if($result){
                return $this->result(config('status.success'), '修改成功', '/admin/index/index');
            }
            return $this->show(config('status.error'), '修改失败');
        }
        return View::fetch('save');
    }

    /**
     * 功能说明 退出登录
     * @author: wang
     * @Date: 2020/3/31 16:08
     */
    public function logout()
    {
        session(null);
        $this->redirect(url('/admin/login/index'));
    }


}